<?php
##############################################
#
#   参与抽奖记录
# @ Time : 2017-1-30 - 下午11:24:18
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Home\Model;

class PartUserModel extends IndexBaseModel{
	/*参与抽奖用户表 */
	protected $tableName = 'part_user';
	protected $startTime;
	protected $endTime;

	public function __construct(){
		$this->startTime = strtotime(date('Y-m-d 00:00:00', time()));
		$this->endTime = strtotime(date('Y-m-d 23:59:59', time()));
		parent::__construct($this->tableName);
		
	}

	//插入抽奖记录
	public function insertRecord($user_id){
	    $user = M('users')->where("id = $user_id")->find();
		$data = array(
			'user_id' => $user['id'],
			'user_name' => $user['nickname'],
			'mobile' => $user['user_mobile'],
			'activity_id' => 2,
			'activity_name' => '大转盘',
			//'draw_results' => $arr['prize_type'],
			'time' => time()
		);
		return $this -> add($data);
    }

    //该活动今天已抽奖总数
    public function getCount($activity_id){
    	return $this -> where("activity_id = $activity_id and (time >= ".$this->startTime." and time <= ".$this->endTime.")") -> count();
    }

    //获取用户今天使用的抽奖总数
    public function getUserDayPrizeCount($user_id,$time)
    {
        $map = array();
        $map['user_id'] = $user_id;
        $starttime = $time['starttime'];
        $endtime = $time['endtime'];

        if ($starttime && $endtime)
            $map['time'] = array('between',array($starttime,$endtime));
        elseif ($starttime)
            $map['time'] = array('egt',$starttime);
        elseif ($endtime)
            $map['time'] = array('elt',$endtime);

        return $result =  M('part_user') ->where($map) -> count();
    	 /*$loop_day = M('lottery_rules')->where('r_id = 1')->getField('loop_day');
    	 //如果循环天数是1按照当天时间统计每个用户每天使用的抽奖机会总数，是0就不按时间统计
    	 if($loop_day == 1){
	        return $result =  $this -> where("(time >= ".$this->startTime." and time <= ".$this->endTime.") and user_id = $user_id") -> count();
         }elseif($loop_day == 0){
         	return $result =  $this -> where("activity_id = 2 and user_id = $user_id") -> count();
         }*/
    }
}
